package com.auxgroup.hr.front.site.co.performance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.site.vo.performance.AssessPersRespVO;
import com.auxgroup.hr.front.site.vo.performance.SaveScoreReqVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.commons.vo.ExamInfoRespVO;
import com.auxgroup.hr.front.commons.vo.QueItemReqVO;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.domain.PsCEvaDocVw;
import com.auxgroup.hr.front.dao.domain.PsCEvaDocVwPrimary;
import com.auxgroup.hr.front.service.performance.AssessService;
import com.auxgroup.hr.front.site.co.BaseCO;
import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.site.vo.performance.AssessPersRespVO;
import com.auxgroup.hr.front.site.vo.performance.SaveScoreReqVO;

/**
 * 测评评估CO
 * 
 * @author wangchangyun
 * @version 创建时间：180527
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/assess")
public class TestAssessCO extends BaseCO {

	private static final Logger logger = LoggerFactory.getLogger(TestAssessCO.class);

	@Autowired
	public AssessService  assessService;
	

	/**
	 * 查询需要测评的人员列表
	 * @param oprid
	 * @param user
	 * @return
	 */
	@GetMapping(value="/persons")
	public BaseResponseVO getPersons(String oprid,String sheetId,String queryKey,@SessionGain("user") UserDTO user){
		logger.info("getWfTerHdr params oprid:{}",oprid);
		if(StringUtils.isBlank(oprid)){
			oprid = user.getOprId();
		}
		List<PsCEvaDocVw> evaPers = assessService.getEvaPersons(oprid,sheetId,queryKey);
		if(evaPers!=null&&!evaPers.isEmpty()){
			Map<String, String> per;
			List<Map<String, String>> pers = new ArrayList<Map<String,String>>();
			AssessPersRespVO respVO = new AssessPersRespVO();
			respVO.setTitle(evaPers.get(0).getDescr());
			for (PsCEvaDocVw eva : evaPers) {
				per = new HashMap<String, String>();
				per.put("name", eva.getName());
				per.put("emplid", eva.getEmplid());
				per.put("dept", eva.getRsFullName());
				per.put("jobDescr", eva.getJobcodeDescr());
				per.put("oprid", eva.getOprid());
				per.put("docid", eva.getcEvaDocId());
				per.put("sheetid", eva.getcEvaSheetId());
				per.put("assessFlag", eva.getcEvaDocStatus2());
				pers.add(per);
			}
			respVO.setPers(pers);
			return buildRespone(respVO,false);
		}
		return buildRespone();
	}
	

	/**
	 * 查询评估考试内容
	 * @param emplid
	 * @return
	 */
	@GetMapping(value="/getExamInfo")
	public BaseResponseVO getExamInfo(String docid,String oprid,String sheetid){
		logger.info("getExamInfo start docid:{},oprid:{},sheetid:{}",docid,oprid,sheetid);
		PsCEvaDocVwPrimary primary = new PsCEvaDocVwPrimary();
		primary.setcEvaDocId(docid);
		primary.setcEvaSheetId(sheetid);
		primary.setOprid(oprid);
		PsCEvaDocVw per = assessService.getEvaPer(primary);
		if(per==null){
			return buildRespone(BaseRetCode.CODE_ERROR_EVA_PERSON_NOT_EXIST);
		}
		ExamInfoRespVO respVO = assessService.getExamInfo(per);
		return buildRespone(respVO,false);
	}
	
	/**
	 * 保存测评分数
	 * @return
	 */
	@PostMapping(value="/saveScore")
	public BaseResponseVO saveExamScore(SaveScoreReqVO reqVO, @SessionGain("user") UserDTO user){
		logger.info("saveExamScore param:{}",reqVO);
		if(StringUtils.isBlank(reqVO.getScoreJson())){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(),"分数信息不能为空");
		}
		List<QueItemReqVO> reqVos = changeToReqVO(reqVO.getScoreJson());
		if(isSameAnswer(reqVos)){
			return buildRespone(BaseRetCode.CODE_ERROR_SELECT_SAME);
		}
		//保存分数信息
		assessService.saveScore(reqVos, reqVO.getDocid(), reqVO.getRole(), reqVO.getEvaluator());
		//计算总分
		assessService.saveDocEvlt(reqVO.getDocid(), reqVO.getRole(), reqVO.getEvaluator(), user.getOprId());
		//调用webservice
		assessService.handleScoreByWebService(reqVO.getDocid());
		return buildRespone(BaseRetCode.CODE_SUCCESS);
	}
	
	
	/**
	 * 转换请求vo
	 * @param scoreJson
	 * @return
	 */
	private List<QueItemReqVO> changeToReqVO(String scoreJson){
		JSONArray reqs = JSONArray.parseArray(scoreJson);
		List<QueItemReqVO> laveReqs = new ArrayList<QueItemReqVO>();
		for (int i = 0; i < reqs.size(); i++) {
			QueItemReqVO reqVo = reqs.getObject(i, QueItemReqVO.class);
			laveReqs.add(reqVo);
		}
		return laveReqs;
	}
	
	
	private Boolean isSameAnswer(List<QueItemReqVO> reqVos){
		Boolean flag = true;
		List<QueItemReqVO> items = new ArrayList<QueItemReqVO>();
		for (QueItemReqVO req : reqVos) {
			if("A".equals(req.getMethod())){
				items.add(req);
			}
		}
		if(!items.isEmpty()){
			String selected = items.get(0).getSelected();
			for (QueItemReqVO item : items) {
				if(!selected.equals(item.getSelected())){
					flag = false;
					break;
				}
			}
			return flag;
		}
		return flag;
	}
	
	
	
}
